package com.zhiying.zfb;

import java.util.Calendar;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	EditText bj;
	EditText zts;
	EditText syts;
	EditText bdll;
	EditText yqnh;
	EditText bxll;
	TextView kbxje;
	TextView bxsxf;
	TextView yflx;
	TextView sjdzje;
	TextView bxhsy;
	TextView bbxsy;
	TextView jlr;
	float bjInt;
	float ztsInt;
	float sytsInt;
	float bdllInt;
	float yqnhInt;
	float bxllInt;
	
	float bxhsyFloat;
	float bbxsyFloat;
	float jlrFloat;
	float sjdzjeFloat;
	float yflxFloat;
	float kbxsxfFloat;
	float kbxjeFloat;
	
	public Button baocun;
	public DBManager dbManager;
	
	public ZfbInfo zfbInfo;
	public Calendar c;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		findView();
		setListeners();
		bj.addTextChangedListener(textChange);
		zts.addTextChangedListener(textChange);
		syts.addTextChangedListener(textChange);
		bdll.addTextChangedListener(textChange);
		yqnh.addTextChangedListener(textChange);
		bxll.addTextChangedListener(textChange);
	}
	
	private void findView() {
		bj = (EditText) findViewById(R.id.bj);
		zts = (EditText) findViewById(R.id.zts);
		syts = (EditText) findViewById(R.id.syts);
		bdll = (EditText) findViewById(R.id.bdll);
		yqnh = (EditText) findViewById(R.id.yqnh);
		bxll = (EditText) findViewById(R.id.bxll);
		kbxje= (TextView) findViewById(R.id.kbxje);
		bxsxf= (TextView) findViewById(R.id.bxsxf);
		yflx = (TextView) findViewById(R.id.yflx);
		sjdzje = (TextView) findViewById(R.id.sjdzje);
		bxhsy = (TextView) findViewById(R.id.bxhsy);
		bbxsy = (TextView) findViewById(R.id.bbxsy);
		jlr = (TextView) findViewById(R.id.jlr);
		
		baocun = (Button) findViewById(R.id.baocun);
		baocun.setEnabled(false);
		dbManager = new DBManager(this);
		zfbInfo = new ZfbInfo();
		c = Calendar.getInstance();
		if("displayitem".equals(getIntent().getAction())) {
			baocun.setVisibility(View.INVISIBLE);
			ZfbInfo zfbInfo = (ZfbInfo)getIntent().getParcelableExtra("zfb");
			bj.setText(zfbInfo.benjin);
			zts.setText(zfbInfo.zongtianshu);
			syts.setText(zfbInfo.shengyutianshu);
			bdll.setText(zfbInfo.gudinglilv);
			yqnh.setText(zfbInfo.yuqililv);
			bxll.setText(zfbInfo.bianxianlilv);
			kbxje.setText(zfbInfo.kebianxian);
			bxsxf.setText(zfbInfo.bianxianshouxufei);
			yflx.setText(zfbInfo.yingfulixi);
			sjdzje.setText(zfbInfo.shijidaozhangjine);
			bxhsy.setText(zfbInfo.bianxianhoushouyi);
			bbxsy.setText(zfbInfo.bubianxianshouyi);
			jlr.setText(zfbInfo.jinglirun);
		}
	}
	
	TextWatcher textChange = new TextWatcher() {
		
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub
			try{
				bjInt = Float.parseFloat(bj.getText().toString());
				ztsInt = Float.parseFloat(zts.getText().toString());
				sytsInt = Float.parseFloat(syts.getText().toString());
				bdllInt = Float.parseFloat(bdll.getText().toString());
				yqnhInt = Float.parseFloat(yqnh.getText().toString());
				bxllInt = Float.parseFloat(bxll.getText().toString());
			} catch(Exception e) {
				
			}
			kbxjeFloat = (1+bdllInt*ztsInt/36500)*bjInt/(1+bxllInt/100*sytsInt/365);
			kbxsxfFloat = (float)(kbxjeFloat * 0.002);
			yflxFloat = kbxjeFloat*bxllInt*sytsInt/36500;
			sjdzjeFloat = kbxjeFloat - kbxsxfFloat;
			
			if(yqnhInt != bdllInt) {
				bbxsyFloat = bjInt*yqnhInt*ztsInt/36500;
				bxhsyFloat = bjInt+bbxsyFloat-yflxFloat-kbxjeFloat;
				jlrFloat = sjdzjeFloat+bxhsyFloat-bjInt;
			} else {
				bxhsyFloat = 0;
				bbxsyFloat = bjInt*bdllInt*ztsInt/36500;
				jlrFloat = sjdzjeFloat - bjInt;
			}
			kbxje.setText(""+kbxjeFloat);
			bxsxf.setText(""+kbxsxfFloat);
			yflx.setText(""+yflxFloat);
			sjdzje.setText(""+sjdzjeFloat);
			bxhsy.setText(""+bxhsyFloat);
			bbxsy.setText(""+bbxsyFloat);
			jlr.setText(""+jlrFloat);	
			baocun.setEnabled(true);
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
			Log.i("zfb","beforeTextChanged");
		}
		
		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			Log.i("zfb","afterTextChanged");
		}
	};
	
	private void setListeners() {
		baocun.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				zfbInfo.setBenjin(bjInt+"");
				zfbInfo.setJinglirun(jlrFloat+"");
				if(c.get(Calendar.MONTH) < 10) {
					if(c.get(Calendar.DATE) < 10) {
						zfbInfo.setRiqi(c.get(Calendar.YEAR)%20+"0"+c.get(Calendar.MONTH)+"0"+c.get(Calendar.DATE));
					} else {
						zfbInfo.setRiqi(c.get(Calendar.YEAR)%20+"0"+c.get(Calendar.MONTH)+""+c.get(Calendar.DATE));
					}
				} else {
					if(c.get(Calendar.DATE) < 10) {
						zfbInfo.setRiqi(c.get(Calendar.YEAR)%20+""+c.get(Calendar.MONTH)+"0"+c.get(Calendar.DATE));
					} else {
						zfbInfo.setRiqi(c.get(Calendar.YEAR)%20+""+c.get(Calendar.MONTH)+""+c.get(Calendar.DATE));
					}
				}
				zfbInfo.setShengyutianshu(sytsInt+"");
				zfbInfo.setYuqililv(yqnhInt+"");
				zfbInfo.setBianxianlilv(bxllInt+"");
				zfbInfo.setKebianxian(kbxjeFloat+"");
				zfbInfo.zongtianshu = ztsInt+"";
				zfbInfo.gudinglilv = bdllInt+"";
				zfbInfo.bianxianshouxufei = kbxsxfFloat+"";
				zfbInfo.yingfulixi = yflxFloat+"";
				zfbInfo.shijidaozhangjine = sjdzjeFloat+"";
				zfbInfo.bianxianhoushouyi = bxhsyFloat+"";
				zfbInfo.bubianxianshouyi = bbxsyFloat+"";
				dbManager.insert(zfbInfo);
				baocun.setEnabled(false);
			}
		});
	}

}
